package com.nf.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DbHelper {
    public static final String driver;
    public static final String url;
    public static final String user;
    public static final String pwd;
    static {
        InputStream stream = DbHelper.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url= properties.getProperty("url");
        user= properties.getProperty("user");
        pwd= properties.getProperty("pwd");
    }
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection connection(){
        try {
            return DriverManager.getConnection(url,user,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void colesConn(Connection connection){
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void colesStmt(PreparedStatement statement){
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void colesSet(ResultSet resultSet){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void colesAll(Connection connection,PreparedStatement statement,ResultSet resultSet){
        colesConn(connection);
        colesStmt(statement);
        colesSet(resultSet);
    }
    public static void colesConnAndStmt(Connection connection,PreparedStatement statement){
        colesConn(connection);
        colesStmt(statement);
    }
}
